package net.maku.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import net.maku.framework.common.utils.Result;
import net.maku.module.entity.AgFriendLink;
import net.maku.module.service.AgFriendLinkService;
import net.maku.module.util.UrlReplaceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/friend-links")
public class AgFriendLinkController {

    @Autowired
    private AgFriendLinkService friendLinkService;

    @Autowired
    private UrlReplaceUtil urlReplaceUtil;

    /**
     * 新增友链
     */
    @PostMapping
    public Result<String> addFriendLink(@RequestBody AgFriendLink friendLink) {
        // 检查必填字段
        if (friendLink.getWebsiteId() == null || friendLink.getLinkFlag() == null) {
            return Result.error("网站ID和链接标识不能为空");
        }
        boolean saved = friendLinkService.save(friendLink);
        return saved ? Result.ok("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改友链
     */
    @PutMapping("/{id}")
    public Result<String> updateFriendLink(@PathVariable Integer id, @RequestBody AgFriendLink friendLink) {
        // 确保 ID 被设置
        friendLink.setId(id);
        boolean updated = friendLinkService.updateById(friendLink);
        return updated ? Result.ok("更新成功") : Result.error("更新失败");
    }

    /**
     * 删除友链
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteFriendLink(@PathVariable Integer id) {
        boolean removed = friendLinkService.removeById(id);
        return removed ? Result.ok("删除成功") : Result.error("删除失败");
    }

    /**
     * 查询所有友链
     */
    @GetMapping
    public Result<List<AgFriendLink>> getAllFriendLinks(@RequestParam(required = false) Integer websiteId,
                                                        @RequestParam(required = false) Integer linkFlag) {
        QueryWrapper<AgFriendLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        if (websiteId != null) {
            queryWrapper.eq("website_id", websiteId);
        }
        if (linkFlag != null) {
            queryWrapper.eq("link_flag", linkFlag);
        }
        List<AgFriendLink> friendLinks = friendLinkService.list(queryWrapper);
        return Result.ok(friendLinks);
    }

    /**
     * 分页查询友链
     */
    @GetMapping("/page")
    public Result<Page<AgFriendLink>> getFriendLinksByPage(@RequestParam(defaultValue = "1") int page,
                                                           @RequestParam(defaultValue = "10") int size,
                                                           @RequestParam(required = false) Integer websiteId,
                                                           @RequestParam(required = false) Integer linkFlag) {
        Page<AgFriendLink> friendLinkPage = new Page<>(page, size);
        QueryWrapper<AgFriendLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        if (websiteId != null) {
            queryWrapper.eq("website_id", websiteId);
        }
        if (linkFlag != null) {
            queryWrapper.eq("link_flag", linkFlag);
        }
        Page<AgFriendLink> resultPage = friendLinkService.page(friendLinkPage, queryWrapper);
        return Result.ok(resultPage);
    }

    @GetMapping("/selectFriendLink")
    public List<AgFriendLink> getFriendLink(HttpServletRequest request, @RequestParam Integer websiteId, @RequestParam(required = false) Integer linkFlag) {
        if(linkFlag == null){
            QueryWrapper<AgFriendLink> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByAsc("sort");
            if (websiteId != null) {
                queryWrapper.eq("website_id", websiteId);
            }
            List<AgFriendLink>  res = friendLinkService.list(queryWrapper);
            urlReplaceUtil.friendLink(request,res);
            return res;
        }else {
            List<AgFriendLink>  res = friendLinkService.getFriendLink(websiteId,linkFlag);
            urlReplaceUtil.friendLink(request,res);
            return res;
        }

    }

}
